<template>
    <div class="d">
        <h1>登录</h1>
        <div class="imgs">
            <img :src=imgUrl alt="" class="showimg">
        </div>
        <el-input v-model="text" placeholder="用户名" />
        <el-input v-model="pwd" placeholder="用户密码" type="password" show-password />
        <div class="con">
            <button class="g" @click="login"><span id="login">登录</span></button>
        </div>
        <a @click="router.push('/login/register')">没有账号？前往注册</a>
        <span class="span1">忘记密码？</span>
        <span @click="router.push('/index')" class="span2">游客登录</span>
    </div>
</template>



<script setup>
import axios from "../axios/index.js";
import { ref } from "vue"
import { useRouter } from "vue-router"
import { ElMessage,ElLoading } from "element-plus"
const router = useRouter()

let imgUrl = ref(require("../assets/img/mrtx.webp"))
let text = ref("")
let pwd = ref("")

//登录按钮点击事件
async function login() {
    if (text.value !== "" && pwd.value !== "") {
        let { data } = await axios.post("/admin/login", {
            userName: text.value,
            userPwd: pwd.value
        })
        if (data === "用户名或密码输入错误") {
            ElMessage.error(data)
            return
        } else {
            imgUrl.value = data.userimg
            
            const loading = ElLoading.service({
                lock: true,
                text: '用户登录成功,即将跳转到首页',
                background: 'rgba(255, 255, 255, 0.7)',
            })
            setTimeout(() => {
                text.value = ""
                pwd.value = ""
                router.push("/index")
                loading.close()
            }, 2000)
            window.localStorage.setItem("id",data.id)
        }
    }
}
</script>

<style lang="less" scoped>
.d {
    .span1 {
        position: absolute;
        bottom: .2rem;
        font-size: .12rem;
        cursor: pointer;
        color: #9c88ff;
    }

    .span2 {
        position: absolute;
        right: 0;
        bottom: .2rem;
        font-size: .12rem;
        cursor: pointer;
        color: #9c88ff;
    }

    >a {
        font-size: .16rem;
        position: absolute;
        bottom: .6rem;
        color: #aaa;
        cursor: pointer;
    }

}

.el-input {
    padding: 0;
    margin-top: .5rem;
    width: 100%;
    outline: none;
    border: none;
    border-bottom: .03rem solid #000;
    font: 900 .16rem '';
    color: #666;
    display: block !important;

    ::v-deep .el-input__wrapper {
        box-shadow: none !important;

        .el-input__inner {
            height: .3rem;
            font-size: .16rem;
        }
    }
}

.d {
    width: 2.5rem;
    height: 5.5rem;
    position: relative;
}

.d h1 {
    margin-top: .2rem;
    font: 900 .3rem '';
}

.f {
    float: right;
    margin: .1rem 0;
    color: #9c88ff;
}

.con {
    margin-top: .4rem;
    display: flex;
    justify-content: space-between;
}

.g {
    color: white !important;
    display: block;
    margin: auto;
    width: 1.2rem;
    height: .6rem;
    border-radius: .3rem;
    border: 0;
    font: 900 .2rem '';
    text-align: center;
    background: linear-gradient(to left, #9c88ff, #3cadeb);
    cursor: pointer;

    span {
        color: white;
    }
}

.g span {
    display: inline-block;
    width: 100%;
    height: 100%;
    line-height: .6rem;
}

.imgs {
    width: 1rem;
    height: 1rem;
    margin: auto;
}

.imgs img {
    width: 1rem;
    height: 1rem;
    border-radius: 100%;
}</style>